const pug = require('pug')
const get_db = require('../get_db')

function get_students() {
    return new Promise((resolve, reject) => {
        const db = get_db()
        db.student.find((err, docs) => {
            // console.log(docs)
            resolve(docs)
        })
    })
}

//Example: /tutorial/student/list
module.exports = async (ctx, namespace, resource, action) => {
    console.log('#4')
    ctx.type = 'text/html'
    if (namespace == 'tutorial' && resource == 'student' && action == 'list') {
        const compiledFunction = pug.compileFile('template/students.pug');
        ctx.body = compiledFunction({
            students: await get_students()
        });
    } else {
        ctx.body = 'Sorry! The app is not supporting this request now!'
    }
}